package com.yhw.srb.sms.receiver;

import com.yhw.srb.base.dto.SmsDTO;
import com.yhw.srb.rabbitmq.constant.MQConst;
import com.yhw.srb.sms.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@Slf4j
public class SmsReceiver {


    @Resource
    private SmsService smsService;

    //发送的 和 监听的 要一致  exchange交换机  key路由
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConst.QUEUE_SMS_ITEM,durable = "true"),
            exchange = @Exchange(value = MQConst.EXCHANGE_TOPIC_SMS),
            key = {MQConst.ROUTING_SMS_ITEM}
    ))
    public void send(SmsDTO smsDTO) throws IOException {

        String mobile = smsDTO.getMobile();
        String message = smsDTO.getMessage();

        // 模板几
        String templateId = "1";

        // message 要发的内容    time 多少分钟内有效
        // 有效时间
        int time = 10;
        String[] datas = {message,""+time};

        smsService.send(mobile,templateId,datas);

    }

}
